import it.unisa.dia.gas.jpbc.Element;
import pre.PRE;
import secretSharing.ThresholdSecretSharing;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Test1 {



    public static void main(String[] args) throws NoSuchAlgorithmException {
        PRE pre = new PRE();

        ThresholdSecretSharing tss = new ThresholdSecretSharing();

        pre.setup();

        Map<String, Element>  user1 = pre.keyGen();
        Map<String, Element>  user2 = pre.keyGen();
        Map<String, Element>  userProxy = pre.keyGen();
        Element user1_pk = user1.get("pk");
        Element user1_sk = user1.get("sk");
        Element user2_pk = user2.get("pk");
        Element user2_sk = user2.get("sk");
        Element userProxy_sk = userProxy.get("sk");

        String message = "this is plaintext";
        Element message_GT = pre.getGT().newElementFromBytes(message.getBytes());
        List<Element> encryptedMessage =  pre.encrypt(user1_pk,message_GT);

        List<Element> reKey = pre.rkgen(user1_sk,user2_pk);

        byte[] reKey_2_byte =  reKey.toString().getBytes();

        System.out.print("reKey_2_byte:");
        System.out.println(new String(reKey_2_byte));


        byte[][] reKeyShares = tss.createShares(reKey_2_byte,10,5,new SecureRandom());


        byte[] recoverReKey = tss.recoverSecret(reKeyShares[0],reKeyShares[2],reKeyShares[4],reKeyShares[6],reKeyShares[8]);
        System.out.print("recoverReKey:");
        System.out.println(new String(recoverReKey));
        System.out.println("recoverReKey.length:"+recoverReKey.length);







//        recoverReKey.
        List<Element> reEncryptedMessage = pre.reEncrypt(reKey,encryptedMessage,userProxy_sk);


        Element decryptedMessage_GT = pre.decrypt(user2_sk,reEncryptedMessage);


        System.out.println(new String(decryptedMessage_GT.toBytes()));






    }

}
